<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .wrap {
            width: 400px;
            height: 400px;
            background-color: grey;
            margin: 100px;
            overflow: hidden;
            /* position: relative; */
        }

        .sub {
            width: 200px;
            height: 200px;
            background-color: pink;
            margin: 100px;
            overflow: hidden;
            /* position: relative; */
        }

        .box {
            width: 100px;
            height: 100px;
            background-color: yellow;
            padding: 5px;
            margin: 50px;

        }
    </style>
</head>

<body>
    <div class="wrap">
        <div class="sub">
            <div class="box"></div>
        </div>
    </div>
</body>
<script>
    //前提: 父子元素绑定相同类型的事件 


    // 阻止事件冒泡 
    // 缺点: 
    // 浏览器 默认的事件传播机制 事件冒泡,但是有时候 并不希望子元素触发事件之后 继续向外层传播,这个时候就需要阻止事件冒泡

    // 常规浏览器
    // e.stopPropagation()

    // IE低版本
    // e.cancelBubble = true;

    // 兼容

    // if(e.stopPropagation){
    //     e.stopPropagation()
    // }else{
    //     e.cancelBubble = true;
    // }





    var wrap = document.getElementsByClassName("wrap")[0];
    var sub = document.getElementsByClassName("sub")[0];
    var box = document.getElementsByClassName("box")[0];

    window.onclick = function () {
        console.log("window被点击")
    }
    document.onclick = function () {
        console.log("document被点击")
    }
    document.documentElement.onclick = function () {
        console.log("html被点击");
    }
    document.body.onclick = function () {
        console.log("body被点击");
    }

    // 双击
    document.body.ondblclick = function () {
        console.log("body被双击");
    }

    wrap.onclick = function (e) {
        if (e.stopPropagation) {
            e.stopPropagation()
        } else {
            e.cancelBubble = true;
        }

        console.log("wrap被点击");
    }

    sub.onclick = function () {
        console.log("sub被点击");
    }

    // 阻止事件冒泡(双击)
    box.ondblclick = function (e) {
        if (e.stopPropagation) {
            e.stopPropagation()
        } else {
            e.cancelBubble = true;
        }

        console.log("box被双击");
    }

    // 阻止事件冒泡(单击)
    box.onclick = function (e) {
        if (e.stopPropagation) {
            e.stopPropagation()
        } else {
            e.cancelBubble = true;
        }

        console.log(e);

        console.log("box被点击");

    }











</script>

</html>